<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
  Class: ReadyPool
  
    &mdash; Documentation by YARD 0.8.7.6
  
</title>

  <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />

  <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />

<script type="text/javascript" charset="utf-8">
  hasFrames = window.top.frames.main ? true : false;
  relpath = '';
  framesUrl = "frames.html#!ReadyPool.html";
</script>


  <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>

  <script type="text/javascript" charset="utf-8" src="js/app.js"></script>


  </head>
  <body>
    <div id="header">
      <div id="menu">
  
    <a href="_index.html">Index (R)</a> &raquo;
    
    
    <span class="title">ReadyPool</span>
  

  <div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
</div>

      <div id="search">
  
    <a class="full_list_link" id="class_list_link"
        href="class_list.html">
      Class List
    </a>
  
    <a class="full_list_link" id="method_list_link"
        href="method_list.html">
      Method List
    </a>
  
    <a class="full_list_link" id="file_list_link"
        href="file_list.html">
      File List
    </a>
  
</div>
      <div class="clear"></div>
    </div>

    <iframe id="search_frame"></iframe>

    <div id="content"><h1>Class: ReadyPool
  
  
  
</h1>

<dl class="box">
  
    <dt class="r1">Inherits:</dt>
    <dd class="r1">
      <span class="inheritName">Object</span>
      
        <ul class="fullTree">
          <li>Object</li>
          
            <li class="next">ReadyPool</li>
          
        </ul>
        <a href="#" class="inheritanceTree">show all</a>
      
      </dd>
    
  
  
    
  
    
  
  
  
    <dt class="r2 last">Defined in:</dt>
    <dd class="r2 last">lib/ready_pool.rb</dd>
  
</dl>
<div class="clear"></div>

<h2>Overview</h2><div class="docstring">
  <div class="discussion">
    
<p>thread pool implementation</p>

<p>In the example 10 threads are prepared to run the Proc passed as the 
second argument. If more are needed they are spun up when required. When a
thread completes its task it returns itself to the pool.</p>

<p>The start method passes its argument to the thread Proc.</p>

<h2>Example</h2>

<p>require 'ready_pool' require 'async_emitter'</p>

<p>emitter = AsyncEmitter.new</p>

<p>emitter.on :data, lambda { |data| puts "emitted #data" }</p>

<p>rp = ReadyPool.new 10, Proc.new { |data| emitter.emit :data, data }</p>

<p>20.times do |i| 	rp.start i end</p>

<p>gets #wait for user input</p>

<p>rp.kill_all</p>


  </div>
</div>
<div class="tags">
  

</div>






  
    <h2>
      Instance Method Summary
      <small>(<a href="#" class="summary_toggle">collapse</a>)</small>
    </h2>

    <ul class="summary">
      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#initialize-instance_method" title="#initialize (instance method)">- (ReadyPool) <strong>initialize</strong>(num_threads, procedure) </a>
    

    
  </span>
  
  
    <span class="note title constructor">constructor</span>
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>ReadyPool constructor.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#kill_all-instance_method" title="#kill_all (instance method)">- (Object) <strong>kill_all</strong> </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>kills all threads.</p>
</div></span>
  
</li>

      
        <li class="public ">
  <span class="summary_signature">
    
      <a href="#start-instance_method" title="#start (instance method)">- (Thread) <strong>start</strong>(data) </a>
    

    
  </span>
  
  
  
  
  
  
  

  
    <span class="summary_desc"><div class='inline'>
<p>starts the thread.</p>
</div></span>
  
</li>

      
    </ul>
  

<div id="constructor_details" class="method_details_list">
  <h2>Constructor Details</h2>
  
    <div class="method_details first">
  <h3 class="signature first" id="initialize-instance_method">
  
    - (<tt><span class='object_link'><a href="" title="ReadyPool (class)">ReadyPool</a></span></tt>) <strong>initialize</strong>(num_threads, procedure) 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>ReadyPool constructor</p>


  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>num_threads</span>
      
      
        <span class='type'>(<tt>FixedNum</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>initial number of threads</p>
</div>
      
    </li>
  
    <li>
      
        <span class='name'>procedure</span>
      
      
        <span class='type'>(<tt>Proc</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>called when start method is called</p>
</div>
      
    </li>
  
</ul>


</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/ready_pool.rb', line 41</span>

<span class='kw'>def</span> <span class='id identifier rubyid_initialize'>initialize</span> <span class='lparen'>(</span><span class='id identifier rubyid_num_threads'>num_threads</span><span class='comma'>,</span> <span class='id identifier rubyid_procedure'>procedure</span><span class='rparen'>)</span>
	<span class='ivar'>@procedure</span> <span class='op'>=</span> <span class='id identifier rubyid_procedure'>procedure</span>
	<span class='ivar'>@pool_semaphore</span> <span class='op'>=</span> <span class='const'>Mutex</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
	<span class='ivar'>@pool_condition</span> <span class='op'>=</span> <span class='const'>ConditionVariable</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span>
	<span class='ivar'>@pool</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>

	<span class='ivar'>@pool_semaphore</span><span class='period'>.</span><span class='id identifier rubyid_synchronize'>synchronize</span> <span class='kw'>do</span>
		<span class='id identifier rubyid_num_threads'>num_threads</span><span class='period'>.</span><span class='id identifier rubyid_times'>times</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_i'>i</span><span class='op'>|</span>
			<span class='ivar'>@pool</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_new_thread'>new_thread</span>
			<span class='ivar'>@pool</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span><span class='lbracket'>[</span><span class='symbol'>:ready</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='kw'>false</span>
			
			<span class='ivar'>@pool</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span><span class='lbracket'>[</span><span class='symbol'>:thread</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='const'>Thread</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='kw'>do</span> 
				<span class='id identifier rubyid_thread_proc'>thread_proc</span> <span class='ivar'>@pool</span><span class='lbracket'>[</span><span class='id identifier rubyid_i'>i</span><span class='rbracket'>]</span>
			<span class='kw'>end</span>

			<span class='ivar'>@pool_condition</span><span class='period'>.</span><span class='id identifier rubyid_wait'>wait</span> <span class='ivar'>@pool_semaphore</span>

		<span class='kw'>end</span>
	<span class='kw'>end</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
  
</div>


  <div id="instance_method_details" class="method_details_list">
    <h2>Instance Method Details</h2>

    
      <div class="method_details first">
  <h3 class="signature first" id="kill_all-instance_method">
  
    - (<tt>Object</tt>) <strong>kill_all</strong> 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>kills all threads</p>


  </div>
</div>
<div class="tags">
  

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


94
95
96
97
98
99</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/ready_pool.rb', line 94</span>

<span class='kw'>def</span> <span class='id identifier rubyid_kill_all'>kill_all</span>
	<span class='ivar'>@pool</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_th'>th</span><span class='op'>|</span>
		<span class='const'>Thread</span><span class='period'>.</span><span class='id identifier rubyid_kill'>kill</span> <span class='id identifier rubyid_th'>th</span><span class='lbracket'>[</span><span class='symbol'>:thread</span><span class='rbracket'>]</span>
	<span class='kw'>end</span>
	<span class='ivar'>@pool</span> <span class='op'>=</span> <span class='lbracket'>[</span><span class='rbracket'>]</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
      <div class="method_details ">
  <h3 class="signature " id="start-instance_method">
  
    - (<tt>Thread</tt>) <strong>start</strong>(data) 
  

  

  
</h3><div class="docstring">
  <div class="discussion">
    
<p>starts the thread</p>


  </div>
</div>
<div class="tags">
  <p class="tag_title">Parameters:</p>
<ul class="param">
  
    <li>
      
        <span class='name'>data</span>
      
      
        <span class='type'>(<tt>Object</tt>)</span>
      
      
      
        &mdash;
        <div class='inline'>
<p>data passed to the thread Proc</p>
</div>
      
    </li>
  
</ul>

<p class="tag_title">Returns:</p>
<ul class="return">
  
    <li>
      
      
        <span class='type'>(<tt>Thread</tt>)</span>
      
      
      
    </li>
  
</ul>

</div><table class="source_code">
  <tr>
    <td>
      <pre class="lines">


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89</pre>
    </td>
    <td>
      <pre class="code"><span class="info file"># File 'lib/ready_pool.rb', line 68</span>

<span class='kw'>def</span> <span class='id identifier rubyid_start'>start</span> <span class='id identifier rubyid_data'>data</span>
	<span class='id identifier rubyid_th'>th</span> <span class='op'>=</span> <span class='kw'>nil</span>
	<span class='ivar'>@pool_semaphore</span><span class='period'>.</span><span class='id identifier rubyid_synchronize'>synchronize</span> <span class='kw'>do</span>
		<span class='id identifier rubyid_th'>th</span> <span class='op'>=</span> <span class='ivar'>@pool</span><span class='period'>.</span><span class='id identifier rubyid_shift'>shift</span>
	<span class='kw'>end</span>

	<span class='kw'>if</span> <span class='id identifier rubyid_th'>th</span> <span class='op'>==</span> <span class='kw'>nil</span>
		<span class='id identifier rubyid_th'>th</span> <span class='op'>=</span> <span class='id identifier rubyid_new_thread'>new_thread</span>
		<span class='id identifier rubyid_th'>th</span><span class='lbracket'>[</span><span class='symbol'>:ready</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='kw'>false</span>
		<span class='id identifier rubyid_th'>th</span><span class='lbracket'>[</span><span class='symbol'>:thread</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='const'>Thread</span><span class='period'>.</span><span class='id identifier rubyid_new'>new</span> <span class='kw'>do</span>
			<span class='id identifier rubyid_thread_proc'>thread_proc</span> <span class='id identifier rubyid_th'>th</span>
		<span class='kw'>end</span>
			
		<span class='ivar'>@pool_semaphore</span><span class='period'>.</span><span class='id identifier rubyid_synchronize'>synchronize</span> <span class='kw'>do</span>
			<span class='ivar'>@pool_condition</span><span class='period'>.</span><span class='id identifier rubyid_wait'>wait</span> <span class='ivar'>@pool_semaphore</span>
		<span class='kw'>end</span>
	<span class='kw'>end</span>

	<span class='id identifier rubyid_th'>th</span><span class='lbracket'>[</span><span class='symbol'>:data</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_data'>data</span>
	<span class='id identifier rubyid_signal_thread'>signal_thread</span> <span class='id identifier rubyid_th'>th</span>
	<span class='kw'>return</span> <span class='id identifier rubyid_th'>th</span><span class='lbracket'>[</span><span class='symbol'>:thread</span><span class='rbracket'>]</span>
<span class='kw'>end</span></pre>
    </td>
  </tr>
</table>
</div>
    
  </div>

</div>

    <div id="footer">
  Generated on Fri May  1 09:30:42 2015 by
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
  0.8.7.6 (ruby-1.9.3).
</div>

  </body>
</html>